﻿using System;
using System.Collections.Generic;
using System.Text;
using AMS.DataAccess.Trademark.Interfaces;
using System.Data;
using AMS.DataAccess.Trademark.Filters;
using AMS.DataAccess.Sql;
using AMS.DataAccess.Common;
using AMS.DataAccess.Trademark.BO;

namespace AMS.DataAccess.Trademark
{
    public class RFTaskManager : IRFTaskManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public DataTable GetRFTasks(RFTaskFilter filter)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT rf.*, task.*, rftask.Type as StartType");
            sql.Add("FROM TD_TMRFCategory rf");
            sql.Add("   LEFT JOIN TD_TMRFTask rftask ON rf.RFIndex = rftask.RFIndex");
            sql.Add("   INNER JOIN TD_TMTaskCategory task ON task.TaskIndex = rftask.TaskIndex");

            if (filter != null)
            {
                sql.Add("WHERE 1=1");
                if (filter.RFIndex != null)
                {
                    sql.Add("    AND rf.RFIndex ", filter.RFIndex.ToString());
                }
                if (filter.CaseType != null)
                {
                    sql.Add("    AND rf.CaseType ", filter.CaseType.ToString());
                }
            }

            sql.Add("ORDER BY rf.RFIndex ASC");

            AMS.DataAccess.SqlDebugger.WriteLine(sql);

            var ds = SqlDBHelper.Query(sql.ToString());
            return ds.Tables[0];
        }

        public DataTable GetTaskDocs(TaskDocFilter filter)
        {
            throw new NotImplementedException();
        }


        public DataTable GetCaseTypes()
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT * FROM TD_TMCaseCategory");
            var ds = SqlDBHelper.Query(sql.ToString());
            return ds.Tables[0];
        }


        public DataTable GetCaseRFTask(string caseTypeID)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT rf.*, task.*, rftask.Type as StartType, caserftask.StepOrder");
            sql.Add("FROM TD_TMRFCategory rf");
            sql.Add("   INNER JOIN TD_TMCaseRFTask caserftask ON rf.RFIndex = caserftask.RFIndex AND ", new SqlExpression("caserftask.CaseType", caseTypeID));
            sql.Add("   LEFT JOIN TD_TMRFTask rftask ON rf.RFIndex = rftask.RFIndex");
            sql.Add("   INNER JOIN TD_TMTaskCategory task ON task.TaskIndex = rftask.TaskIndex");

            sql.Add("ORDER BY caserftask.StepOrder ASC");

            AMS.DataAccess.SqlDebugger.WriteLine(sql);

            var ds = SqlDBHelper.Query(sql.ToString());
            return ds.Tables[0];
        }

        public void InsertRFToCaseProcess(string caseID, string rfIndex, int index)
        {
            if (string.IsNullOrEmpty(caseID))
            {
                throw new ArgumentNullException("caseID");
            }
            if (string.IsNullOrEmpty(rfIndex))
            {
                throw new ArgumentNullException("rfIndex");
            }
            if (index < 0)
            {
                throw new ArgumentException("index 不能小于0");
            }

            SqlBatch batch = new SqlBatch();
            SqlUpdate update = new SqlUpdate("TD_TMCaseRFTask");
            update.AddColumn("StepOrder = StepOrder + 1");
            update.AddCondition(new SqlCondition("StepOrder", index, SqlOperations.LargerThanOrEquals));
            batch.Add(update);

            SqlInsert insert = new SqlInsert("TD_TMCaseRFTask");
            insert.AddColumn("CaseType", caseID);
            insert.AddColumn("RFIndex", rfIndex);
            insert.AddColumn("StepOrder", index);
            batch.Add(insert);


            System.Diagnostics.Debug.WriteLine(batch.ToString());

            SqlDBHelper.ExecuteSql(batch.ToString());
        }

        public void AddRFToCase(string caseID, string rfIndex)
        {
            int index = 0;
            SqlBuilder sqlGetIndex = new SqlBuilder();
            sqlGetIndex.Add("SELECT TOP 1 StepOrder FROM TD_TMCaseRFTask");
            sqlGetIndex.Add("WHERE ", new SqlExpression("CaseType", caseID));
            sqlGetIndex.Add("   ORDER BY StepOrder DESC");

            System.Diagnostics.Debug.WriteLine(sqlGetIndex.ToString());

            object oData = SqlDBHelper.GetSingle(sqlGetIndex.ToString());
            if (oData == null || oData == DBNull.Value)
            {
                index = 0;
            }
            else
            {
                index = (int)oData;
                index++;
            }

            SqlInsert insert = new SqlInsert("TD_TMCaseRFTask");
            insert.AddColumn("CaseType", caseID);
            insert.AddColumn("RFIndex", rfIndex);
            insert.AddColumn("StepOrder", index);

            AMS.DataAccess.SqlDebugger.WriteLine(insert);

            SqlDBHelper.ExecuteSql(insert.ToString());
        }

        public void DeleteRFFromCase(string caseID, string rfIndex)
        {
            SqlBatch batch = new SqlBatch();
            SqlUpdate update = new SqlUpdate("TD_TMCaseRFTask");
            update.AddColumn("StepOrder = StepOrder - 1");
            SqlBuilder sqlQryIndex = new SqlBuilder();
            sqlQryIndex.Add("SELECT TOP 1 StepOrder FROM TD_TMCaseRFTask");
            sqlQryIndex.Add("WHERE ", new SqlExpression("CaseType", caseID));
            sqlQryIndex.Add("    AND ", new SqlExpression("RFIndex", rfIndex));

            update.AddCondition(string.Format("StepOrder > ({0})", sqlQryIndex.ToString()));
            batch.Add(update);

            SqlDelete delete = new SqlDelete("TD_TMCaseRFTask");
            delete.AddCondition(new SqlCondition("CaseType", caseID));
            delete.AddCondition(new SqlCondition("RFIndex", rfIndex));
            batch.Add(delete);


            System.Diagnostics.Debug.WriteLine(batch.ToString());

            SqlDBHelper.ExecuteSql(batch.ToString());
        }


        public void SwapNodeOrderIndex(string caseID, string rfIndexA, int rfOrderA, string rfIndexB, int rfOrderB)
        {
            SqlBatch batch = new SqlBatch();
            SqlUpdate update = new SqlUpdate("TD_TMCaseRFTask");
            update.AddColumn("StepOrder", rfOrderB);
            update.AddCondition(new SqlCondition("CaseType", caseID));
            update.AddCondition(new SqlCondition("RFIndex", rfIndexA));
            batch.Add(update);

            update = new SqlUpdate("TD_TMCaseRFTask");
            update.AddColumn("StepOrder", rfOrderA);
            update.AddCondition(new SqlCondition("CaseType", caseID));
            update.AddCondition(new SqlCondition("RFIndex", rfIndexB));
            batch.Add(update);

            System.Diagnostics.Debug.WriteLine(batch.ToString());

            SqlDBHelper.ExecuteSql(batch.ToString());
        }


        public BO.RFCategoryInfo GetRecvFile(string rfIndex)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT * FROM TD_TMRFCategory");
            sql.Add("WHERE ", new SqlExpression("RFIndex", rfIndex));

            var ds = SqlDBHelper.Query(sql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return BusinessObjectMapping.DataRowToBO<RFCategoryInfo>(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public TaskCategoryInfo GetTask(string taskIndex)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT * FROM TD_TMTaskCategory");
            sql.Add("WHERE ", new SqlExpression("TaskIndex", taskIndex));

            var ds = SqlDBHelper.Query(sql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return BusinessObjectMapping.DataRowToBO<TaskCategoryInfo>(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public bool UpdateRecvFile(RFCategoryInfo recvfile)
        {
            SqlUpdate update = new SqlUpdate("TD_TMRFCategory");
            update.AddBusinessObject(recvfile);
            update.AddCondition(new SqlCondition("RFIndex", recvfile.RFIndex));

            AMS.DataAccess.SqlDebugger.WriteLine(update);

            return 0 < SqlDBHelper.ExecuteSql(update.ToString());
        }

        public void AddRecvFile(RFCategoryInfo recvFile)
        {
            SqlInsert sql = new SqlInsert("TD_TMRFCategory");
            sql.AddBusinessObject(recvFile);

            AMS.DataAccess.SqlDebugger.WriteLine(sql);

            SqlDBHelper.ExecuteSql(sql.ToString());
        }

        public void DeleteRecvFile(string rfIndex)
        {
            SqlDelete sql = new SqlDelete("TD_TMRFCategory");
            sql.AddCondition(new SqlCondition("RFIndex", rfIndex));

            AMS.DataAccess.SqlDebugger.WriteLine(sql);

            SqlDBHelper.ExecuteSql(sql.ToString());
        }


        public List<TaskCategoryInfo> GetTasks(TaskCategoryFilter filter)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT * FROM TD_TMTaskCategory");
            if (filter != null)
            {
                if (filter.TaskName != null)
                {
                    sql.Add("WHERE ", new SqlCondition("TaskName", filter.TaskName.ToString()));
                }
            }

            AMS.DataAccess.SqlDebugger.WriteLine(sql);

            var ds = SqlDBHelper.Query(sql.ToString());
            if (ds != null && ds.Tables[0] != null)
            {
                return BusinessObjectMapping.DataTableToBOList<TaskCategoryInfo>(ds.Tables[0]);
            }

            return null;
        }


        public void AddTask(TaskCategoryInfo task)
        {
            SqlInsert sql = new SqlInsert("TD_TMTaskCategory");
            sql.AddBusinessObject(task);

            AMS.DataAccess.SqlDebugger.WriteLine(sql);

            SqlDBHelper.ExecuteSql(sql.ToString());
        }

        public bool UpdateTask(TaskCategoryInfo task)
        {
            SqlUpdate update = new SqlUpdate("TD_TMTaskCategory");
            update.AddBusinessObject(task);
            update.AddCondition(new SqlCondition("TaskIndex", task.TaskIndex));

            AMS.DataAccess.SqlDebugger.WriteLine(update);

            return 0 < SqlDBHelper.ExecuteSql(update.ToString());
        }

        public void DeleteTask(string taskIndex)
        {
            SqlDelete sql = new SqlDelete("TD_TMTaskCategory");
            sql.AddCondition(new SqlCondition("TaskIndex", taskIndex));

            AMS.DataAccess.SqlDebugger.WriteLine(sql);

            SqlDBHelper.ExecuteSql(sql.ToString());
        }
    }
}
